package com.wang.springbootinit.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.springbootinit.common.BaseResponse;
import com.wang.springbootinit.common.ResultUtils;
import com.wang.springbootinit.model.entity.Grade;
import com.wang.springbootinit.model.param.GradeParam;
import com.wang.springbootinit.model.param.GradeUpdateParam;
import com.wang.springbootinit.model.vo.GradeStudentVO;
import com.wang.springbootinit.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/grade")
public class AdminGradeController {

    @Autowired
    private GradeService gradeService;

    @GetMapping("/list")
    public BaseResponse<Page<GradeStudentVO>> listGrades(
            @RequestParam(required = false) String className,
            @RequestParam(required = false) String courseName,
            @RequestParam(required = false) String semester,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return ResultUtils.success(gradeService.listGrades(className, courseName, semester, page, size));
    }

    @PostMapping("/add")
    public BaseResponse<Boolean> createGrade(@RequestBody GradeParam param) {
        return ResultUtils.success(gradeService.saveGrade(param));
    }

    @PostMapping("/update/{id}")
    public BaseResponse<Grade> updateGrade(
            @PathVariable Long id,
            @RequestBody Grade grade) {
        return ResultUtils.success(gradeService.updateGrade(id, grade));
    }

    @PostMapping("/updateBatch")
    public BaseResponse<Boolean> updateBatch(@RequestBody GradeUpdateParam param) {
        return ResultUtils.success(gradeService.updateBatch(param));
    }

    @PostMapping("/delete/{id}")
    public BaseResponse<Void> deleteGrade(@PathVariable Long id) {
        gradeService.deleteGrade(id);
        return ResultUtils.success();
    }

    @GetMapping("/get")
    public BaseResponse<List<Grade>> getGrade(@RequestParam Long studentId) {
        return ResultUtils.success(gradeService.getGradeByStudentId(studentId));
    }

    @GetMapping("/getOptions")
    public BaseResponse<Map<String, List<String>>> getSemesterOptions() {
        return ResultUtils.success(gradeService.getOptions());
    }

    @GetMapping("/student/{studentId}")
    public BaseResponse<List<Grade>> getStudentGrades(
            @PathVariable Long studentId) {
        return ResultUtils.success(gradeService.getStudentGrades(studentId));
    }

    @GetMapping("/student/{studentId}/semester/{semester}")
    public BaseResponse<List<Grade>> getStudentSemesterGrades(
            @PathVariable Long studentId,
            @PathVariable String semester) {
        return ResultUtils.success(gradeService.getStudentSemesterGrades(studentId, semester));
    }

    @GetMapping("/course/{courseName}")
    public BaseResponse<List<Grade>> getCourseGrades(
            @PathVariable String courseName) {
        return ResultUtils.success(gradeService.getCourseGrades(courseName));
    }

    @PostMapping("/import")
    public BaseResponse<String> importGrades(@RequestParam("file") MultipartFile file) {
        try {
            int count = gradeService.importGradesFromExcel(file);
            return ResultUtils.success("成功导入 " + count + " 条成绩记录");
        } catch (Exception e) {
            return ResultUtils.error("导入失败：" + e.getMessage());
        }
    }

    @GetMapping("/export")
    public BaseResponse<byte[]> exportGrades(
            @RequestParam(required = false) Long studentId,
            @RequestParam(required = false) String semester,
            @RequestParam(required = false) String courseName) {
        
        byte[] excelContent = gradeService.exportGradesToExcel(studentId, semester, courseName);
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", "grades.xlsx");
        
        return ResultUtils.success(excelContent);
    }


} 